<template>
  <div class='container'>
    <!-- 标签名称栏 -->
    <el-row type="flex" class="row-bg" justify="seb">
      <el-col :span="16">
        <div class="grid-content bg-purple">
          <el-form size="mini" :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item label="标签名称">
              <el-input v-model="form.tagName" placeholder="审批人"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="form.state" placeholder="活动区域">
                <!-- <el-option label="不筛选" value=''></el-option> -->
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="onCancel">取消</el-button>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <!--新增标签  -->
      <el-col :span="8">
        <div class="grid-right bg-purple">
          <el-button type="text" @click="$router.push('list')" v-show="tiaozhuangshow">返回学科</el-button>
          <el-button type="warning" size="mini" @click="showAddEmptDialogFn">新增标签</el-button>
        </div>
      </el-col>
    </el-row>
    <!-- 数据一共15条 -->
    <el-row>
      <el-col :span="24">
        <div class="grid-content bg-purple-dark"><i class="el-icon-info"></i>数据一共{{ counts }}条</div>
      </el-col>
    </el-row>
    <!-- 数据渲染 -->
    <el-row>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科" width="180">
        </el-table-column>
        <el-table-column prop="tagName" label="标签名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者">
        </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template slot-scope="{row}">
            {{ row.addDate | parseTimeByString("{y}-{m}-{d}") }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template slot-scope="{row}">
            {{ row.state ? '已禁用' : '已启用' }}
          </template>
        </el-table-column>
        <el-table-column prop="state" label="操作">
          <template slot-scope="{row}">
            <el-button type="text" @click="onDisable(row)">
              <span v-if="row.state === 1">启用</span>
              <span v-else>禁用</span>
            </el-button>
            <el-button type="text" :disabled="!row.state" @click="onRevise(row)">修改</el-button>
            <el-button type="text" :disabled="!row.state" @click="onDel(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
    <!-- 分页 -->
    <el-row type="flex" justify="end">
      <el-pagination background :page-size="form.pagesize" :page-sizes="[5, 10, 20, 50]" @size-change="handleSizeChange"
        @current-change="onCurrentChange" layout="prev, pager, next,jumper,sizes" :total="counts"
        :current-page="form.page">
      </el-pagination>
    </el-row>
    <!-- 子组件 -->
    <tags-add :show-dialog.sync="showAddEmptDialog" @success="success" :addto="date" ref="tagsRefs"></tags-add>
  </div>
</template>

<script>
import { list as listAPI } from '@/api/hmmm/tags.js'
import { remove } from '@/api/hmmm/tags.js'
import { changeState } from '@/api/hmmm/tags.js'
import tagsAdd from '../components/tags-add.vue'

export default {
  components: { tagsAdd },
  name: 'Tags',
  data () {
    return {
      tiaozhuangshow:false, // 跳回学科
      showAddEmptDialog: false, //新增标签弹窗
      form: {
        page: 1,
        pagesize: 10,
        tagName: "",
        state: '', //状态
        subjectID:undefined
      },
      date: {
        id: "",
        tagName: '',
        subjectID: ''
      },
      counts: 0,
      //分页页码
      formInline: {
        user: '',
        region: ''
      },
      tableData: []
    }
  },
  created () {
    const id = this.$route.query.id
    const name = this.$route.query.name
    if (id && name) {
      this.tiaozhuangshow = true
      this.form.subjectID = id
    }
  //  console.log(id)
    this.list()
  },
  methods: {
    // 新增标签
    success () {
      this.list()
    },
    // 删除
    onDel (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await remove(row)
        console.log(res)
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.list()
        } else {
          this.$message({
            type: 'error',
            message: '删除失败!'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },
    // 修改内容
    onRevise (row) {
      // console.log(row, '11')
      this.date.id = row.id
      this.date.tagName = row.tagName
      this.date.subjectID = row.subjectID
      this.showAddEmptDialog = true
      this.$refs.tagsRefs.Fnet(row)
    },
    // 禁用按钮
    async onDisable (row) {
      try {
        const { data } = await changeState({
          id: row.id,
          state: row.state === 0 ? 1 : 0
        })
        console.log(data)
        if (data.success) {
          this.$message.success('操作成功')
          this.list()
        } else {
          this.$message.error('操作失败')
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 前往分页
    handleSizeChange (value) {
      this.form.pagesize = value
      this.list(this.form.pagesize)
    },
    // 分页事件
    onCurrentChange (value) {
      // console.log(value)
      this.form.page = value
      this.list(this.form)
    },
    // 调用接口
    async list (value = {}) {
      value = Object.assign({}, this.form, value)
      if (value.state === '') {
        delete value.state
      }
      if (!value.tagName) {
        delete value.tagName
      }
      const { data } = await listAPI(value)
      // console.log(data)
      this.tableData = data.items
      this.counts = data.counts
    },
    // 查询按钮
    async onSubmit () {
      this.list(this.form)
    },
    // 取消按钮
    onCancel () {
      this.form = {
        page: 1,
        pagesize: 10,
        tagName: "",
        state: '',
      }
      this.list()
    },
    // 新增方法
    showAddEmptDialogFn () {
      this.showAddEmptDialog = true
      this.$refs.tagsRefs.form = {
        subjectID: undefined,
        tagName: '',
      }
    },
  },
}
</script>

<style scoped lang='less'>
.container {
  background-color: #fff;
  padding: 20px;

  .bg-purple-dark {
    background: #f4f4f5;

    .el-icon-info {
      margin-right: 8px;
    }
  }

  .grid-content {
    display: flex;
    padding: 8px;
    font-size: 16px;
    border-radius: 5px;
  }

  .grid-right {
    display: flex;
    justify-content: end;
  }

  .demo-input-size {
    display: flex;
    justify-content: space-between;
  }
}
</style>
